home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / misc / frefs11.lha / FetchRefs / Source / GenerateIndex / Main.c < prev    next >
C/C++ Source or Header  |  1994-08-04  |  8KB  |  269 lines

  1. /**************************************************************************/
  2. /* Main.c - startup, etc.                                                 */
  3. /**************************************************************************/
  4.  
  5. #include "GenerateIndex.h"
  6.  
  7. extern struct WBStartup *_WBMsg;
  8. extern void _waitwbmsg(void);
  9.  
  10. STRPTR InitialFile;
  11.  
  12. const UBYTE VersTag[] = "$VER: GenerateIndex " VERSION " " DATE;
  13.  
  14. const UBYTE Template[] =  "FROM/M,TO,SETTINGS,"
  15.                     "RECURSIVELY/S,KEEPEMPTY/S,UNRECOGAREDOCS/S,"
  16.                     "AUTODOC/S,"
  17.                     "C/S,C_DEFINE/S,C_STRUCT/S,C_TYPEDEF/S,"
  18.                     "E/S,E_CONST/S,E_OBJECT/S,E_PROC/S,"
  19.                     "ASM/S,ASM_EQU/S,ASM_STRUCTURE/S,ASM_MACRO/S";
  20.  
  21. const UBYTE CLI_Help[] = "\n"
  22.                    "GenerateIndex [[FROM] {wildcard}] [TO <file>] [SETTINGS <file>]\n"
  23.                    "\t[RECURSIVELY] [KEEPEMPTY] [UNRECOGAREDOCS]\n"
  24.                    "\t[AUTODOC]\n"
  25.                    "\t[C] [C_DEFINE] [C_STRUCT] [C_TYPEDEF]\n"
  26.                    "\t[E] [E_CONST] [E_OBJECT] [E_PROC]\n"
  27.                    "\t[ASM] [ASM_EQU] [ASM_STRUCTURE] [ASM_MACRO]\n"
  28.                    "\n"
  29.                    "- Not specifying FROM will open the GUI (ReqTools and Triton required)\n"
  30.                    "- A script is handy for Shell usage\n"
  31.                    "- Study the guide for more information\n"
  32.                    "\n";
  33.  
  34. struct List FileList;
  35. struct RDArgs *Args;
  36. struct ReqToolsBase *ReqToolsBase;
  37. ULONG ParseResult[LAST_ARGUMENT], Options;
  38.  
  39. /// _main( ) -- handle parsing of WB/Shell arguments
  40. __stkargs LONG
  41. _main(LONG argc, STRPTR argv)
  42. {
  43.     ReqToolsBase = OpenLibrary("reqtools.library", 38);
  44.     
  45.     NewList(&FileList);
  46.  
  47.     if (_WBMsg)
  48.     {
  49.         struct DiskObject *diskobj;
  50.  
  51.         CurrentDir(_WBMsg->sm_ArgList->wa_Lock);
  52.         if (diskobj = GetDiskObject(_WBMsg->sm_ArgList->wa_Name))
  53.         {
  54.             STRPTR arg;
  55.  
  56.             if (arg = FindToolType(diskobj->do_ToolTypes, "TO"))
  57.                 if (InitialFile = AllocVec(strlen(arg) + 1, NULL))
  58.                     strcpy(InitialFile, arg);
  59.  
  60.             if (arg = FindToolType(diskobj->do_ToolTypes, "SETTINGS"))
  61.                 LoadSettings(arg);
  62.             else
  63.                 LoadSettings("ENV:FetchRefs_GI.prefs");
  64.  
  65.             FreeDiskObject(diskobj);
  66.  
  67.             GoGUI();
  68.         } else
  69.             CloseAll(IoErr());
  70.     } else
  71.     {
  72.         if (Args = AllocDosObject(DOS_RDARGS, NULL))
  73.         {
  74.             Args->RDA_ExtHelp = CLI_Help; /* Activate extended help */
  75.  
  76.             if (ReadArgs(Template, ParseResult, Args)) /* Parse arguments */
  77.             {
  78.                 /* Set options */
  79.                 if (ParseResult[SETTINGS])
  80.                     LoadSettings((STRPTR)ParseResult[SETTINGS]);
  81.                 else
  82.                 {
  83.                     if (ParseResult[AUTODOC])
  84.                         Settings.AutoDocPrf.Active = TRUE;
  85.  
  86.                     if (ParseResult[CINC])
  87.                         Settings.CPrf.Active = TRUE;
  88.                     if (ParseResult[C_DEFINE])
  89.                         Settings.CPrf.Define = TRUE;
  90.                     if (ParseResult[C_STRUCT])
  91.                         Settings.CPrf.Struct = TRUE;
  92.                     if (ParseResult[C_TYPEDEF])
  93.                         Settings.CPrf.Typedef = TRUE;
  94.  
  95.                     if (ParseResult[EINC])
  96.                         Settings.EPrf.Active = TRUE;
  97.                     if (ParseResult[E_CONST])
  98.                         Settings.EPrf.Const = TRUE;
  99.                     if (ParseResult[E_OBJECT])
  100.                         Settings.EPrf.Object = TRUE;
  101.                     if (ParseResult[E_PROC])
  102.                         Settings.EPrf.Proc = TRUE;
  103.  
  104.                     if (ParseResult[ASMINC])
  105.                         Settings.AsmPrf.Active = TRUE;
  106.                     if (ParseResult[ASM_EQU])
  107.                         Settings.AsmPrf.Equ = TRUE;
  108.                     if (ParseResult[ASM_STRUCTURE])
  109.                         Settings.AsmPrf.Structure = TRUE;
  110.                     if (ParseResult[ASM_MACRO])
  111.                         Settings.AsmPrf.Macro = TRUE;
  112.  
  113.                     if (ParseResult[RECURSIVELY])
  114.                         Settings.Recursively = TRUE;
  115.                     if (ParseResult[KEEPEMPTY])
  116.                         Settings.KeepEmpty = TRUE;
  117.                     if (ParseResult[UNRECOGAREDOCS])
  118.                         Settings.UnknownFiles = UNKNOWN_ARE_AUTODOCS;
  119.                 }
  120.  
  121.                 if (ParseResult[TO])
  122.                 {
  123.                     if  (!ParseResult[FROM])
  124.                     {
  125.                         if (InitialFile = AllocVec(strlen((STRPTR)ParseResult[TO]) + 1, NULL))
  126.                             strcpy(InitialFile, (STRPTR)ParseResult[TO]);
  127.                     } else
  128.                         LoadData((STRPTR)ParseResult[TO]);
  129.                 }
  130.  
  131.                 if  (!ParseResult[FROM])
  132.                     GoGUI();
  133.                 else if (ParseResult[TO])
  134.                 {
  135.                     /* Expand each wildcard (which can also be just a file) and generate index */
  136.                     ULONG count;
  137.                     STRPTR path;
  138.                     struct AnchorPath *fanchor;
  139.  
  140.                     if (fanchor = AllocVec(sizeof(struct AnchorPath) + 256, MEMF_CLEAR))
  141.                     {
  142.                         fanchor->ap_Strlen = 256;
  143.                         for (count = 0; path = ((UBYTE **)ParseResult[FROM])[count]; count++)
  144.                         {
  145.                             if (!MatchFirst(path, fanchor))
  146.                             {
  147.                                 do
  148.                                 {
  149.                                     STRPTR name;
  150.                                     
  151.                                     if (name = FullName(fanchor->ap_Buf))
  152.                                     {
  153.                                         if (fanchor->ap_Info.fib_DirEntryType < 0)
  154.                                             IndexFile(name, "");
  155.                                         else
  156.                                             IndexRecursive(fanchor->ap_Buf, "");
  157.                                         FreeVec(name);
  158.                                     } else
  159.                                         CloseAll(IoErr());
  160.                                 } while (!MatchNext(fanchor));
  161.                                 MatchEnd(fanchor);
  162.                             }
  163.                         }
  164.                         FreeVec(fanchor);
  165.                         SaveData((STRPTR)ParseResult[TO]);
  166.                     } else
  167.                         CloseAll(ERROR_NO_FREE_STORE);
  168.                  } else
  169.                     CloseAll(ERROR_REQUIRED_ARG_MISSING);
  170.             } else
  171.                 CloseAll(IoErr());
  172.         } else
  173.             CloseAll(ERROR_NO_FREE_STORE);
  174.     }
  175.     CloseAll(0);
  176. }
  177. ///
  178.  
  179. /// LoadSettings()
  180. void
  181. LoadSettings(STRPTR file)
  182. {
  183.     BPTR fil;
  184.  
  185.     if (fil = Open(file, MODE_OLDFILE))
  186.     {
  187.         Read(fil, &Settings, sizeof(Settings));
  188.         Close(fil);
  189.     }
  190. }
  191. ///
  192. /// SaveSettings()
  193. void
  194. SaveSettings(STRPTR file)
  195. {
  196.     BPTR fil;
  197.  
  198.     if (fil = Open(file, MODE_NEWFILE))
  199.     {
  200.         Write(fil, &Settings, sizeof(Settings));
  201.         Close(fil);
  202.     }
  203. }
  204. ///
  205.  
  206. /// CloseAll(LONG error [, STRPTR errtxt])
  207. void
  208. CloseAll(LONG error, ...)
  209. {
  210.     STRPTR errtxt;
  211.     va_list args;
  212.  
  213.     va_start(args, error);
  214.     errtxt = *(STRPTR *)args;
  215.     va_end(args);
  216.  
  217.     FreeVec(InitialFile);
  218.     
  219.     if (Args)
  220.     {
  221.         FreeArgs(Args);
  222.         FreeDosObject(DOS_RDARGS, Args);
  223.     }
  224.  
  225.     ClearFileList();
  226.  
  227.     if (error)
  228.     {
  229.         UBYTE errortxt[80];
  230.         static struct EasyStruct errreq = {
  231.             sizeof(struct EasyStruct),
  232.             0,
  233.             "GenerateIndex " VERSION " by Anders Melchiorsen",
  234.             "%s\n(Error code #%ld)",
  235.             "Quit"
  236.         };
  237.  
  238.         if (error == ERROR_SPECIAL)
  239.         {
  240.             strcpy(errortxt, errtxt);
  241.             errreq.es_TextFormat[2] = 0;
  242.         } else
  243.             Fault(error, "GenerateIndex", errortxt, 80);
  244.  
  245.         if (_WBMsg)
  246.             EasyRequest(NULL, &errreq, NULL, errortxt, error);
  247.         else
  248.         {
  249.             PutStr(errortxt);
  250.             PutStr("\n");
  251.         }
  252.     }
  253.  
  254.     CloseLibrary(&ReqToolsBase->LibNode);
  255.     
  256.     _exit(error ? 5 : 0);
  257.  
  258.     _waitwbmsg(); /* Dummy reference; never actually called */
  259. }
  260. ///
  261.  
  262. /// chkabort(void) -- disable DICE ^C check
  263. void
  264. chkabort(void)
  265. {
  266.     return;
  267. }
  268. ///
  269.